package com.guoguo.controller;

import com.alibaba.fastjson.JSONObject;
import com.guoguo.base.BaseController;
import com.guoguo.base.PageBean;
import com.guoguo.base.PaginationContext;
import com.guoguo.entity.Role;
import com.guoguo.service.RoleService;
import com.guoguo.utils.UUIDUtils;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * Created by KLSD on 2017/9/13.
 */
@Controller
@RequestMapping("/view")
public class RoleController extends BaseController {
    @Autowired
    private RoleService roleService;
    @RequestMapping(value="/roleList",method= RequestMethod.GET)
    public ModelAndView role(@RequestParam int pageNumber,@RequestParam int pageSize){
        Map<String, Object> data = new HashedMap();
        PageBean<Role> role=roleService.selectRoleList(pageNumber,pageSize);
        data.put("roleList",role);
        return  returnView("/roleList",data);


    }

    /**
     *去跳转新增修改角色
     * @param roleId
     * @return
     */
    @RequestMapping(value="/saveRole",method = RequestMethod.GET)
    public ModelAndView saveRole(String roleId){
        Map<String,Object> data=new HashedMap();
        if("".equals(roleId)||roleId==null){
            data.put("roleId", UUIDUtils.getUUID());

        }else{
            Role role=roleService.selectByPrimaryKey(roleId);
            data.put("roleId",role.getRoleId());
            data.put("roleName",role.getRoleName());
            data.put("roleDescription",role.getRoleDescription());
//           ;
        }
        return  returnView("/role/roleAdd",data);
    }

    @RequestMapping(value="/saveOrupdateRole",method = RequestMethod.POST)
    public JSONObject updateRole(@RequestParam String roleId,@RequestParam String roleName,@RequestParam String roleDescription){
      Role role=roleService.addRole(roleId,roleName,roleDescription);
        return  returnResult(200,role,"");


    }

    /**
     * 删除角色
     * @param roleId
     * @return
     */
    @RequestMapping(value="/deleteRole",method = RequestMethod.GET)
    public ModelAndView deleteRole(String roleId){
        int i=roleService.deleteByPrimaryKey(roleId);
        PageBean<Role> role=roleService.selectRoleList(PaginationContext.getPageNum(), PaginationContext.getPageSize());
        Map<String, Object> data = new HashedMap();
        data.put("roleList",role);
        return  returnView("/roleList",data);
    }
}
